import 'dart:io';

import 'package:core_package/core_package.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/main/splash_view_page.dart';
import 'package:flutter_app_unionad/request/login_request.dart';
import 'package:flutter_app_unionad/router/routes.dart';
import 'package:flutter_app_unionad/ui/login/login_for_mobile_page.dart';
import 'package:flutter_app_unionad/ui/my_facility/recharge_page.dart';
import 'package:flutter_app_unionad/ui/partnership/facility_list_page.dart';
import 'package:flutter_app_unionad/view_model/page_manager_view_model.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';
import 'package:sharesdk_plugin/sharesdk_interface.dart';
import 'package:sharesdk_plugin/sharesdk_register.dart';
import 'package:fluwx/fluwx.dart';

import 'main_screen.dart';
import 'package:jpush_flutter/jpush_flutter.dart';

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: "汼汼生活",
      theme: AppTheme.norTheme,

      //普通模式主题
      darkTheme: AppTheme.darkTheme,
      initialRoute: Routes.initialRoute,
      builder: (context, child) {
        return MediaQuery(
          data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
          child: child ?? Container(),
        );
      },
      // home: LoginForMobilePage(),
      onGenerateRoute: Routes.router!.generator,

      routes: {
        Routes.initialRoute: (ctx) {
          // return LoginForMobilePage();

          return Consumer<PagesManagerViewModel>(
            builder: (context, model, child) {
              print("刷新app-------->>> ${model.type} ");
              if (model.type == PagesManagerType.login) {
                return LoginForMobilePage();
              } else if (model.type == PagesManagerType.launch) {
                return SplashViewPage();
              }
              return MainScreen();
            },
          );
        }
      },
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate
      ],
      supportedLocales: [
        const Locale('zh', 'CN'),
        const Locale('en', 'US'),
      ],
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    String? agreeProtocol = UserCacheUil.getString('Protocol');
    if ((agreeProtocol != null && agreeProtocol.length != 0)) {
      print('注册 -------------11');

      initPlatformState();

      ShareSDKRegister register = ShareSDKRegister();
      register.setupWechat('wx4344437e7b2e124f', '030576778c54620985823f88d74bb34d', 'https://51uuid.com/');
      SharesdkPlugin.regist(register);
      registerWxApi(
          appId: "wx4344437e7b2e124f", doOnAndroid: true, doOnIOS: true, universalLink: "https://51uuid.com/");
    }
  }

  final JPush jpush = new JPush();

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    jpush.cleanTags();

    jpush.setup(
      appKey: "cd2797ac35abdbd9f6da4e71", //你自己应用的 AppKey
      channel: "developer-default",
      production: true,
      debug: true,
    );
    jpush.applyPushAuthority(new NotificationSettingsIOS(sound: true, alert: true, badge: false));

    // Platform messages may fail, so we use a try/catch PlatformException.
    jpush.getRegistrationID().then((rid) {
      print("flutter get registration id : $rid"); //1a1018970a6049da468
      ///记录注册id
      UserCacheUil.setString(Configuration.jsPushRegisterId, rid);

      ///服务器保存  jsPush 注册id
      LoginRequest.saveJSPushRegister();
      // setState(() {
      //   debugLable = "flutter getRegistrationID: $rid";
      // });
    });
  }
}
